package com.hg.oa.module.home.layer;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hg.oa.R;
import com.hg.oa.common.restful.ErrorResponse;
import com.hg.oa.common.restful.RestResponse;
import com.hg.oa.common.restful.RestResponseListener;
import com.hg.oa.module.calendar.layer.DayInputActivity;
import com.hg.oa.module.daka.layer.DakaActivity;
import com.hg.oa.module.home.adapter.HomeRecycleViewAdapter;
import com.hg.oa.module.home.model.Book;
import com.hg.oa.module.home.request.HomeRequest;
import com.hg.oa.util.ActivityHelper;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by fengxiang on 2016/9/9.
 */
public class HomeFragment extends Fragment implements HomeRecycleViewAdapter.OnMenuClickListener {

    String tag = "HomeFragment";
    Unbinder unbinder;

    @BindView(R.id.recycleView)
    RecyclerViewFinal recycleView;

    HomeRecycleViewAdapter adapter;
    LinearLayoutManager layoutManager;

    View headerView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        headerView = view.findViewById(R.id.header_menu);
        unbinder = ButterKnife.bind(this, view);
        adapter = new HomeRecycleViewAdapter(this);
        layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(layoutManager);
        adapter.setOnMenuClickListener(this);
        recycleView.setAdapter(adapter);
        recycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int position = layoutManager.findFirstVisibleItemPosition();
                if (position >= 1) {
                    headerView.setVisibility(View.VISIBLE);
                } else {
                    headerView.setVisibility(View.GONE);
                }
            }
        });
        getBooks();
        return view;
    }

    void getBooks() {
        Log.e(tag, "getBooks");
        new HomeRequest().books(new RestResponseListener<RestResponse<List<Book>>>() {
            @Override
            public void onSuccess(Call<RestResponse<List<Book>>> call, Response<RestResponse<List<Book>>> response) {
                RestResponse<List<Book>> restResponse = response.body();
                Log.e(tag, restResponse.toString());
            }

            @Override
            public void onError(Call<RestResponse<List<Book>>> call, ErrorResponse t) {
                Log.e(tag, t.getMessage());
            }
        });
    }

    @OnClick(R.id.text_qiandao)
    void qiandao() {
        Intent intent = new Intent(getActivity(), DakaActivity.class);
        ActivityHelper.startBySceneTransitionAnimation(getActivity(), intent);
    }


    @OnClick(R.id.text_input_dayreport)
    void dayreport() {
        Intent intent = new Intent(getActivity(), DayInputActivity.class);
        intent.putExtra("type", DayInputActivity.TYPE_REPORT);
        ActivityHelper.startBySceneTransitionAnimation(getActivity(), intent);
    }


    @OnClick(R.id.text_input_level)
    void level() {
        Intent intent = new Intent(getActivity(), DayInputActivity.class);
        intent.putExtra("type", DayInputActivity.TYPE_LEVEL);
        ActivityHelper.startBySceneTransitionAnimation(getActivity(), intent);
    }


    @Override
    public void onMenuClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.text_qiandao:
                intent = new Intent(getActivity(), DakaActivity.class);
                ActivityHelper.startBySceneTransitionAnimation(getActivity(), intent);
                break;
            case R.id.text_input_dayreport:
                intent = new Intent(getActivity(), DayInputActivity.class);
                intent.putExtra("type", DayInputActivity.TYPE_REPORT);
                ActivityHelper.startBySceneTransitionAnimation(getActivity(), intent);
                break;
            case R.id.text_input_level:
                intent = new Intent(getActivity(), DayInputActivity.class);
                intent.putExtra("type", DayInputActivity.TYPE_LEVEL);
                ActivityHelper.startBySceneTransitionAnimation(getActivity(), intent);
                break;
            case R.id.text_chart:
                //  intent = new Intent(getActivity(), DakaActivity.class);
                //   startActivity(intent);
                break;
        }
    }

}
